package learn.arc.mybatis;

import com.alibaba.druid.pool.DruidPooledPreparedStatement;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.ibatis.executor.BaseExecutor;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.executor.parameter.ParameterHandler;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.ParameterMapping;
import org.apache.ibatis.plugin.*;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.scripting.defaults.DefaultParameterHandler;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.type.TypeHandlerRegistry;
import org.springframework.stereotype.Component;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
import java.sql.PreparedStatement;
import java.text.DateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Properties;
import java.util.regex.Matcher;

/**
 *
 * MyBatis拦截器打印不带问号的完整sql语句
 * 只在开发环境下执行
 * https://blog.csdn.net/hunt_er/article/details/120081742
 *
 * 加入项目 https://blog.csdn.net/weixin_43596905/article/details/126366922
 *  @Component。自定义配置数据源，@Component方式失效
 *  xml配置。
 *  ConfigurationCustomizer。
 *
 */
@Slf4j
@Component
//@ConditionalOnProperty(value = "spring.profiles.active", havingValue = "dev")
@Intercepts({
        @Signature(type = Executor.class, method = "update", args = {MappedStatement.class,
                Object.class}),
        @Signature(type = Executor.class, method = "query", args = {MappedStatement.class,
                Object.class, RowBounds.class, ResultHandler.class})})
public class ExcutorSqlInterceptor implements Interceptor {


    public void debugPoint(){
        BaseExecutor be;
//        System.out.println(MybatisSQLResolverInterceptor.showSql(ms.configuration, boundSql)); return false;
    }

    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        try {
            // 获取xml中的一个select/update/insert/delete节点，是一条SQL语句
            MappedStatement mappedStatement = (MappedStatement) invocation.getArgs()[0];
            Object parameter = null;
            // 获取参数，if语句成立，表示sql语句有参数，参数格式是map形式
            if (invocation.getArgs().length > 1) {
                parameter = invocation.getArgs()[1];
            }
            String id = mappedStatement.getId(); // 获取到节点的id,即sql语句的id
            BoundSql boundSql = mappedStatement.getBoundSql(parameter); // BoundSql就是封装myBatis最终产生的sql类
            Configuration configuration = mappedStatement.getConfiguration(); // 获取节点的配置
            String sql = showSql(configuration, boundSql); // 获取到最终的sql语句
            log.debug("mybatis拦截解析: \n ID: \t{}\n SQL: \t{}", id, sql);
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 执行完上面的任务后，不改变原有的sql执行过程
        return invocation.proceed();
    }

    /**
     * 进行？的替换
     */
    public static String showSql(Configuration configuration, BoundSql boundSql) {
        // 获取参数
        Object parameterObject = boundSql.getParameterObject();
        List<ParameterMapping> parameterMappings = boundSql.getParameterMappings();
        // sql语句中多个空格都用一个空格代替
        String sql = boundSql.getSql().replaceAll("[\\s]+", " ");
        if (CollectionUtils.isNotEmpty(parameterMappings) && parameterObject != null) {
            // 获取类型处理器注册器，类型处理器的功能是进行java类型和数据库类型的转换
            TypeHandlerRegistry typeHandlerRegistry = configuration.getTypeHandlerRegistry();
            // 如果根据parameterObject.getClass(）可以找到对应的类型，则替换
            String value = getParameterValue(parameterObject);
            if (typeHandlerRegistry.hasTypeHandler(parameterObject.getClass())) {
                sql = sql.replaceFirst("\\?",
                        Matcher.quoteReplacement(value));
            } else {
                // MetaObject主要是封装了originalObject对象，提供了get和set的方法用于获取和设置originalObject的属性值,主要支持对JavaBean、Collection、Map三种类型对象的操作
                MetaObject metaObject = configuration.newMetaObject(parameterObject);
                for (ParameterMapping parameterMapping : parameterMappings) {
                    String propertyName = parameterMapping.getProperty();
                    if (metaObject.hasGetter(propertyName)) {
                        Object obj = metaObject.getValue(propertyName);
                        sql = sql.replaceFirst("\\?",
                                Matcher.quoteReplacement(value));
                    } else if (boundSql.hasAdditionalParameter(propertyName)) {
                        // 该分支是动态sql
                        Object obj = boundSql.getAdditionalParameter(propertyName);
                        sql = sql.replaceFirst("\\?",
                                Matcher.quoteReplacement(value));
                    } else {
                        // 未知参数，替换？防止错位
                        sql = sql.replaceFirst("\\?", "unknown");
                    }
                }
            }
        }
        return sql;
    }

    // https://blog.csdn.net/weixin_41037319/article/details/117335049
    public static boolean parameterHandlerDebug(PreparedStatement ps) throws Throwable {
        // System.out.println(((PreparedStatementProxyImpl) ((DruidPooledPreparedStatement) ps).stmt).raw); return false;
        // return MybatisSQLResolverInterceptor.parameterHandlerDebug(ps);
        ParameterHandler debugPoint;
        DefaultParameterHandler h1;
        PreparedStatement sqlStatement = ps;
        String psClassName = ps.getClass().getName();
        System.out.println(psClassName);
        if (Proxy.isProxyClass(ps.getClass())) {
            InvocationHandler handler = Proxy.getInvocationHandler(ps);
            if (handler.getClass().getName().endsWith(".PreparedStatementLogger")) {
                Field field = handler.getClass().getDeclaredField("ps");
                field.setAccessible(true);
                sqlStatement = (PreparedStatement) field.get(handler);
                // 原文参考：https://www.zhangshengrong.com/p/OgN5DgLDan/
                // 以下应该是使用了 druid 原因 ，没有使用的话下面几行代码请注释掉
//                Field stmt = sqlStatement.getClass().getDeclaredField("stmt");
//                stmt.setAccessible(true);
//                sqlStatement = (PreparedStatement) stmt.get(sqlStatement);
//                Field clientStatement = sqlStatement.getClass().getDeclaredField("ps");
//                clientStatement.setAccessible(true);
//                sqlStatement = (PreparedStatement) clientStatement.get(sqlStatement);
            }
        }else if(psClassName.contains("DruidPooledPreparedStatement")){
            sqlStatement = ((DruidPooledPreparedStatement) ps).getRawPreparedStatement();
        }
        System.out.println(sqlStatement.toString().substring(sqlStatement.toString().indexOf(":") + 1));

        return false;
    }

    /**
     * 如果参数是String，则添加单引号
     * 如果参数是日期，则转换为时间格式器并加单引号； 对参数是null和不是null的情况作了处理
     */
    private static String getParameterValue(Object obj) {
        String value;
        if (obj instanceof String) {
            value = "'" + obj.toString() + "'";
        } else if (obj instanceof Date) {
            DateFormat formatter = DateFormat.getDateTimeInstance(DateFormat.DEFAULT,
                    DateFormat.DEFAULT, Locale.CHINA);
            value = "'" + formatter.format(new Date()) + "'";
        } else {
            if (obj != null) {
                value = obj.toString();
            } else {
                value = "";
            }
        }
        return value;
    }


    @Override
    public Object plugin(Object target) {
        return Plugin.wrap(target, this);
    }

    @Override
    public void setProperties(Properties properties) {
        // NOP
    }

}
